Page({
  data: {
    showDebugButton: false,
    showStorageModal: false,
    storageAnalysis: {}
  },

  onLoad() {

  },


  onLogout() {
    // 保存用户注册数据，只清空用户登录状态
    const usersData = wx.getStorageSync('badminton_users');
    
    // 清空存储，但保留用户注册数据
    wx.clearStorageSync();
    
    // 恢复用户注册数据
    if (usersData) {
      wx.setStorageSync('badminton_users', usersData);
    }
    
    getApp().globalData.userInfo = null;
    wx.showToast({ title: '已退出登录' });
    wx.reLaunch({ url: '/pages/index/index' });
  },
  
  onBack() {
    wx.navigateBack();
  },
  
  onUserAgreement() {
    wx.navigateTo({
      url: '/pages/disclaimer/disclaimer'
    });
  },

  // 调试功能：查看数据存储信息
  onDebugData() {
    try {
      const localDataManager = require('../../utils/localDataManager.js');
      
      // 获取详细存储分析
      const storageAnalysis = localDataManager.getDetailedStorageAnalysis();
      
      if (!storageAnalysis) {
        wx.showToast({ title: '获取存储信息失败', icon: 'none' });
        return;
      }
      
      // 获取用户数据
      const userInfo = wx.getStorageSync('userInfo');
      const users = wx.getStorageSync('badminton_users');
      const userCount = users ? users.length : 0;
      
      // 获取存储信息
      let storageInfo = {};
      try {
        storageInfo = wx.getStorageInfoSync();
      } catch (error) {
        storageInfo = { currentSize: 0, limitSize: 10240 };
      }
      
      const actualStorageSize = parseFloat(storageAnalysis.total.sizeKB);
      const isOverLimit = actualStorageSize > (storageInfo.limitSize / 1024);
      
      // 构建存储分析数据
      const analysisData = {
        races: {
          sizeKB: storageAnalysis.files.races.sizeKB,
          count: storageAnalysis.files.races.count
        },
        signups: {
          sizeKB: storageAnalysis.files.signups.sizeKB,
          count: storageAnalysis.files.signups.count
        },
        matches: {
          sizeKB: storageAnalysis.files.matches.sizeKB,
          count: storageAnalysis.files.matches.count
        },
        total: {
          sizeKB: storageAnalysis.total.sizeKB
        },
        userCount: userCount,
        userStatus: userInfo ? '已登录' : '未登录',
        limit: (storageInfo.limitSize / 1024).toFixed(2),
        isOverLimit: isOverLimit
      };
      
      console.log('=== 详细存储分析 ===');
      console.log(storageAnalysis);
      
      this.setData({
        showStorageModal: true,
        storageAnalysis: analysisData
      });
      
    } catch (error) {
      console.error('调试信息获取失败:', error);
      wx.showToast({ title: '调试失败', icon: 'none' });
    }
  },

  // 关闭存储分析弹窗
  onCloseStorageModal() {
    this.setData({
      showStorageModal: false
    });
  },

  // 阻止事件冒泡
  stopPropagation() {
    // 空函数，用于阻止事件冒泡
  },

  // 清理存储数据
  onCleanupStorage() {
    wx.showModal({
      title: '清理存储',
      content: '这将删除所有比赛、报名和比赛数据。确定要清理吗？',
      confirmText: '确定清理',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          try {
            const localDataManager = require('../../utils/localDataManager.js');
            const success = localDataManager.clearAllData();
            
            if (success) {
              wx.showToast({ title: '清理成功', icon: 'success' });
              // 关闭弹窗
              this.setData({
                showStorageModal: false
              });
            } else {
              wx.showToast({ title: '清理失败', icon: 'none' });
            }
          } catch (error) {
            console.error('清理存储失败:', error);
            wx.showToast({ title: '清理失败', icon: 'none' });
          }
        }
      }
    });
  }
});